package com.mvc.finance.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.mvc.finance.model.ExpenseApplyItem;
import com.mvc.framework.model.Auditable;
import com.mvc.framework.service.BaseService;

@Service
public class ExpenseApplyItemManager extends BaseService<ExpenseApplyItem, Integer> {
	public List<ExpenseApplyItem> getExpenseApplyItemsByExpenseApplyId(int expenseApplyId) {
		ExpenseApplyItem example = new ExpenseApplyItem();
		example.setExpenseApplyId(expenseApplyId);
		example.setRecStatus(Auditable.REC_STATUS_VALID);
		return getHibernateTemplate().findByExample(example);
	}

	public void deleteByBankStatementId(int expenseApplyId) {
		deleteByWhere("expenseApplyId = " + expenseApplyId);
	}

	public void saveBankStatementItem(ExpenseApplyItem orderItem, String operation) throws Exception {
		if (operation.equals("del")) {
			super.delete(orderItem.getId());
		} else if (operation.equals("add")) {
			orderItem.setId(null);
			super.save(orderItem);
		} else if (operation.equals("edit")) {
			super.save(orderItem);
		}
	}

	public void updateExpenseApplyItemId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + ExpenseApplyItem.class.getName() + " set expenseApplyId = "
		        + newOrderId + " where expenseApplyId = " + oldOrderId);
		query.executeUpdate();
	}
}
